﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class PrintCategoryObject
    {
        public static PrintCategory[] GetPrintCategories()
        {
            return Query(null);
        }

        public static PrintCategory GetPrintCategory(int id)
        {
            PrintCategory printCategory = new PrintCategory();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrintCategoryDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable printCategoryies = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            printCategory.Id                = Convert.ToInt32(printCategoryies.Rows[0][PrintCategoryDesc.Id]);
            printCategory.TemplateId        = Convert.ToInt32(printCategoryies.Rows[0][PrintCategoryDesc.TemplateId]);
            printCategory.Category          = Convert.ToInt32(printCategoryies.Rows[0][PrintCategoryDesc.Category]);
            printCategory.Name              = Convert.ToString(printCategoryies.Rows[0][PrintCategoryDesc.Name]);
            printCategory.PrinterQueueId    = Convert.ToInt32(printCategoryies.Rows[0][PrintCategoryDesc.PrinterQueueId]);
            printCategory.InUse             = Convert.ToInt32(printCategoryies.Rows[0][PrintCategoryDesc.InUse]);


            return printCategory;
        }

        public static PrintCategory GetPrintCategory(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintCategoryDesc.Name, ConditionExpress.EQUALS, name);

            PrintCategory[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static PrintCategory[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + PrintCategoryDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                PrintCategory[] printCategories = new PrintCategory[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    PrintCategory printCategory = new PrintCategory();

                    printCategory.Id              = Convert.ToInt32(table.Rows[i][PrintCategoryDesc.Id]);
                    printCategory.TemplateId      = Convert.ToInt32(table.Rows[i][PrintCategoryDesc.TemplateId]);
                    printCategory.Category        = Convert.ToInt32(table.Rows[i][PrintCategoryDesc.Category]);
                    printCategory.Name            = Convert.ToString(table.Rows[i][PrintCategoryDesc.Name]);
                    printCategory.PrinterQueueId  = Convert.ToInt32(table.Rows[i][PrintCategoryDesc.PrinterQueueId]);
                    printCategory.InUse           = Convert.ToInt32(table.Rows[i][PrintCategoryDesc.InUse]);




                    printCategories.SetValue(printCategory, i);
                }

                return printCategories;
            }
            return null;
        }

        public static Int32 Add(PrintCategory printCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(PrintCategoryDesc.TemplateId,             printCategory.TemplateId);
            builder.InsertAdd(PrintCategoryDesc.Category,               printCategory.Category);
            builder.InsertAdd(PrintCategoryDesc.Name,                   printCategory.Name);
            builder.InsertAdd(PrintCategoryDesc.PrinterQueueId,         printCategory.PrinterQueueId);
            builder.InsertAdd(PrintCategoryDesc.InUse,                  printCategory.InUse);

            builder.QueryTableName = PrintCategoryDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrintCategoryDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(PrintCategory printCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(PrintCategoryDesc.TemplateId,             printCategory.TemplateId);
            builder.UpdateAdd(PrintCategoryDesc.Category,               printCategory.Category);
            builder.UpdateAdd(PrintCategoryDesc.Name,                   printCategory.Name);
            builder.UpdateAdd(PrintCategoryDesc.PrinterQueueId,         printCategory.PrinterQueueId);
            builder.UpdateAdd(PrintCategoryDesc.InUse,                  printCategory.InUse);

            builder.ConditionAdd(PrintCategoryDesc.Id, ConditionExpress.EQUALS, printCategory.Id);
            builder.QueryTableName = PrintCategoryDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
